************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Figure 7 (Attendance by Treatment Group over the Course of the Study)
* Last edited: 07 May 2021
************************************************

************************************************	
*1. Initial Setup
************************************************

	clear all
	set more off
	set matsize 800

	use "$d/attendance_dataset", clear
			
*****************************************
* 2. Graphs - Attendance
*****************************************

** Panel A **
	preserve
		
		quietly tab pid if day_in_study !=.
		local pids = r(r)
		
		bysort pid: egen treat_group = max(treatment_group)
		
		gen control_night_present = at_present_check if treat_group == 0
		gen treat_night_present = at_present_check if treat_group == 1 | treat_group == 2
		
		collapse (mean) at_present_check control_night_present treat_night_present , by(day_in_study)
		rename at_present_check frac_present
		
		keep if day_in_study > 1 & day_in_study <= 28
		
		label var frac_present "Fraction Present"
		
		twoway ///
		(line control_night_present day_in_study, lcolor(black) lpattern(dash) sort) ///
		(line treat_night_present day_in_study, lcolor(purple) lpattern(solid) sort) ///
		(line treat_night_present day_in_study if day_in_study == 0, lcolor(black) lpattern(solid) sort) , ///
		ylabel(0.7(0.05)1) ///
		xlabel(2(2)28) ///
		graphregion(fcolor(white)) ///
		xline(8, lc(black)) ///
		legend(order(1 "Night Sleep Control" 2 "Night Sleep Treatment") cols(2) colfirst region(lwidth(none)))
		
			graph export "$oa/Figures/Figure_A7/frac_present_night-sleep_treat.png", replace
			graph export "$oa/Figures/Figure_A7/frac_present_night-sleep_treat.eps", replace
			
	restore
	
** Panel B **	
	preserve	
		quietly tab pid if day_in_study !=.
		local pids = r(r)
		
		bysort pid: egen treat_group = max(treatment_group)
		
		gen control_nap_present = at_present_check if treat_nap == 0
		gen treat_nap_present = at_present_check if treat_nap == 1
		
		collapse (mean) at_present_check control_nap_present treat_nap_present , by(day_in_study)
		
		keep if day_in_study > 1 & day_in_study <= 28
		
		twoway ///
		(line control_nap_present day_in_study, lcolor(black) lpattern(dash) sort) ///
		(line treat_nap_present day_in_study, lcolor(green) lpattern(solid) sort) ///
		(line treat_nap_present day_in_study if day_in_study == 0, lcolor(black) lpattern(solid) sort) , ///
		ylabel(0.7(0.05)1) ///
		xlabel(2(2)28) ///
		graphregion(fcolor(white)) ///
		xline(8, lc(black)) ///
		legend(order(1 "Nap Control" 2 "Nap Treatment") cols(2) colfirst region(lwidth(none)))
		
			graph export "$oa/Figures/Figure_A7/frac_present_nap_treat.png", replace
			graph export "$oa/Figures/Figure_A7/frac_present_nap_treat.eps", replace
	
	restore
